const {
    serverStatus
} = require("./config");

//范围内随机数
const random_number = (min, max) => {
    var range = max - min;
    var rand = Math.random();
    var num = min + Math.round(rand * range);
    return parseInt(num);
}

//模拟负载服务器宕机
const simulateServerStatus = (pools, looptime = 10000) => {
    setInterval(() => {
        const index = random_number(0, 2);
        let flag = '';
        Object.keys(pools).forEach((key, i) => {
            pools[key].status = i !== index ? serverStatus.online : serverStatus.offline
            if(i === index){
                flag = key;
            }
        });
    }, looptime)
}

module.exports = {
    random_number,
    simulateServerStatus
}

